/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.nsi.comm;

import gnu.io.*;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;


public class Connection implements SerialPortEventListener {

	/**
	 * Frame start code
	 */
	public static final String STX = "<STX>";

	/**
	 * Final-frame code
	 */
	public static final String ETX = "<ETX>";

	/**
	 * Intermediate-frame code
	 */
	public static final String EBX = "<EBX>";

	/**
	 * Checksum upper
	 */
	public static final String CSH = "<CSH>";

	/**
	 * Checksum lower
	 */
	public static final String CSL = "<CSL>";

	/**
	 * Frame end code (carriage return + line feed)
	 */
	public static final String CR_LF = "<CR><LF>";
	
	/**
	 * Enquire Connection
	 */
	public static final String ENQ = "<ENQ>"; 
	
	/**
	 * Affirmative acknowledge (acknowledgment)
	 */
	public static final String ACK = "<ACK>" ;

	/**
	 * Negative acknowledge (acknowledgment)
	 */
	public static final String NAK = "<NAK>"; 
	
	/**
	 *  End of transmission
	 */
	public static final String EOT = "<EOT>";
	
	/**
	 * Sample request skip response (equipment control character)
	 */
	public static final String DC1 = "<DC1>";	

	public static void connect() throws Exception {
		
		//impressao i = new impressao();
		//i.imprimi(new String[]{""}, 1, 1, "");
		
            InputStream entrada = null;
		OutputStream saida = null;
		SerialPort porta = null;

		try {
			//captura a lista de portas disponíveis,
			//pelo método estético em CommPortIdentifier.
			Enumeration pList = CommPortIdentifier.getPortIdentifiers();
			//CommPortIdentifier.getPortIdentifier("COM5");
			//Um mapping de nomes para CommPortIdentifiers.
			HashMap map = new HashMap();
	
			CommPortIdentifier cp = null; 
			
			//Procura pela porta desejada
			while (pList.hasMoreElements()) {
		        CommPortIdentifier cpi = (CommPortIdentifier)pList.nextElement();
		        map.put(cpi.getName(), cpi);
		        if (cpi.getPortType() == CommPortIdentifier.PORT_SERIAL) {
		        	cp = cpi;
		                System.err.println("PORTA SERIAL!!!!");
		        }
		        if (cpi.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
		        	System.err.println("PORTA PARALELA!!!!");
		        }
			}
			
			porta = (SerialPort)cp.open("COM1",3000);
			try {
				//setSerialPortParams(int baudrate, int dataBits, int stopBits, int parity)
				porta.setSerialPortParams(9600, SerialPort.DATABITS_8,SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
			} catch (UnsupportedCommOperationException e) {
				e.printStackTrace();
			}
			
	        entrada = porta.getInputStream();
			saida = porta.getOutputStream();
			String msg = "teste de msg";
                         System.out.println("encrevendo...");
			saida.write(msg.getBytes());
                         System.out.println("OK");
	
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			porta.removeEventListener();
			porta.close();
			saida.close();
			entrada.close();
		}
                
                System.out.println("OK");
	}

	public void serialEvent(SerialPortEvent arg0) {
		
	}

	public void ownershipChange(int arg0) {
		
		
		
		
	}
	
	
	
}

